//
//  groupViewController.m
//  mumegui
//
//  Created by Niels Buekers on 12/11/10.
//  Copyright 2010 NB. All rights reserved.
//

#import "groupViewController.h"
#import "cwRadioAppDelegate.h"


@implementation groupViewController


#pragma mark -
#pragma mark Initialization

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	cwRadioAppDelegate *appDelegate = (cwRadioAppDelegate *)[[UIApplication sharedApplication] delegate];
	locController = [[locationController alloc] init];
	locController.delegate = appDelegate;
	[locController.locationManager startUpdatingLocation];

}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return groupsList.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return @"Available groups";
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	[[cell textLabel] setText:(NSString*)[groupsList objectAtIndex:indexPath.row]];
    return cell;
}

//function for setting groups after the parser did its work
-(void) setGroups:(NSMutableArray*) list{
	groupsList = list;
	[self.tableView reloadData];
}

#pragma mark -
#pragma mark Table view delegate

//when selecting a group, we would like to join it
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	UILabel * lbl =[[tableView cellForRowAtIndexPath:indexPath] textLabel];
	NSString* groep = [lbl text];
	cwRadioAppDelegate *appDelegate = (cwRadioAppDelegate *)[[UIApplication sharedApplication] delegate];
	[appDelegate joinGroup:groep];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

